home *** CD-ROM | disk | FTP | other *** search
/ Video Toaster 4.2 / Video Toaster v4.2.iso / arexx / modeler / trajmotion.lwm < prev    next >
Text File  |  1993-12-13  |  4KB  |  165 lines

  1. /* CMD: Trajectory Motion
  2.  * Make Ballistic Trajectory Motion Path for LW
  3.  * By Arnie Cachelin © 1992 NewTek Inc.
  4.  */
  5.  
  6. NUMERIC DIGITS 6
  7. call addlib "LWModelerARexx.port", 0
  8. signal on error
  9. signal on syntax
  10. call addlib "rexxsupport.library", 0, -30, 0
  11. MATHLIB="rexxmathlib.library"
  12. IF POS(MATHLIB , SHOW('L')) = 0 THEN
  13.   IF ~ADDLIB(MATHLIB , 0 , -30 , 0) THEN DO
  14.     call notify(1,"!Can't find "MATHLIB)
  15.     exit
  16.     END
  17.  
  18. sysnam = 'Ballistic Trajectory Maker '
  19. version = 'v1.0'
  20. Frames=30
  21. h=0; p=0; b=0
  22. xsc=1; ysc=1; zsc=1
  23. File.type="LWMO"
  24. SecsPerFrame=1/30
  25. Time=SecsPerFrame*Frames
  26. SlicesPerFrame=10
  27. dt=SecsPerFrame/SlicesPerFrame
  28. g=9.8 /* acceleration of gravity in m/s */
  29.  
  30. call req_begin sysnam
  31.  
  32. id_mes  = req_addcontrol("Make a projectile", 'T', "motion for LW, using real physics!")
  33. id_frames  = req_addcontrol("Frames", 'n')
  34. id_keysp  = req_addcontrol("Key Spacing", 'n')
  35. id_x0 = req_addcontrol("Initial Position", 'v',0)
  36. id_y0 = req_addcontrol("Ground Level  Y = ", 'n',0)
  37. id_elev  = req_addcontrol("Launch Elevation (°)", 'n')
  38. id_dir  = req_addcontrol("Launch Direction (°)", 'n')
  39. id_dE = req_addcontrol("Bounciness", 'n',0) /* used to be called Elasticity, but that was too technical :-O  */
  40. id_T = req_addcontrol("Hang Time (s)", 'n',0)
  41.  
  42. call req_setval id_frames, Frames
  43. call req_setval id_T, Time
  44. call req_setval id_Keysp, 5
  45. call req_setval id_x0,0,0
  46. call req_setval id_dE,.75,.75
  47. call req_setval id_y0,0,0
  48. call req_setval id_elev,75
  49. call req_setval id_dir,0
  50.  
  51. if (~req_post()) then do
  52.     call req_end
  53.     exit
  54. end
  55.  
  56. frames   = req_getval(id_frames) % 1
  57. KeySpacing = req_getval(id_keysp)
  58. theta = req_getval(id_dir)
  59. phi = req_getval(id_elev)
  60. parse value  req_getval(id_x0) with xi yi zi .
  61. y0= req_getval(id_y0)
  62. dE= req_getval(id_dE)
  63. HTime = req_getval(id_dE)
  64.  
  65. call req_end
  66.  
  67. ENVFile=getfilename("-- Save Motion --","motions")
  68. if ENVFile ~="(none)" then File.Name=ENVFile
  69. else exit
  70. /*  WARNING: physics part approaching */
  71. Time=SecsPerFrame*Frames
  72. Keys=1+Frames%KeySpacing
  73. frame=0
  74. slice=0
  75.  
  76. /* For ballistic trajectory, Vx=V*cos(a), Vy=V*sin(a), Y=((Vy)^2)/g, T=2*Vy/g
  77.     ==> X=T*Vx=2*Vy*Vx/g.  Setting shot duration will be the priority, so the
  78.     only input will be the time.  The height, Y, will then be set since:
  79.     Y=((T*g/2)^2)/g =(g*T^2)/4.  Vx is arbitrary, but it determines the angle
  80.     and the distance. To make X=Y, use Vx=Vy/4, which is about 76 degrees.
  81.     I'll add wind a bit later!!!
  82. */
  83.  
  84. /* Initial Conditions */
  85.  
  86. vyi=HTime*g/2
  87. vxi=vyi*tan(phi)
  88. vxi=vxi*cos(theta)
  89. vzi=vxi*sin(theta)
  90. x=xi; y=yi; z=zi
  91. vx=vxi; vy=vyi; vz=vzi
  92.  
  93. call meter_begin Frames*SlicesPerFrame+3, 'Generating Motion'
  94. call WriteHeader(mot,keys)
  95. do t=0 to Time+dt by dt
  96.     if slice//SlicesPerFrame = 0 then do
  97.       if frame//KeySpacing=0 then
  98.         call WriteKey(mot,frame,0)
  99.       else if frame=Frames then
  100.         call WriteKey(mot,frame,0)
  101.       frame=frame+1
  102.     end
  103.     slice=slice+1
  104.     vx=vx+dt*ax(x,y,z)
  105.     vy=vy+dt*ay(x,y,z)
  106.     vz=vz+dt*az(x,y,z)
  107.     x=x + dt*vx
  108.     z=z + dt*vz
  109.     y=y + dt*vy
  110.     if y<=y0 then do  /* Bounce! */
  111.       y=y - dt*vy     /* Jump up to prev y */
  112.       vy=-dE*vy       /* reverse velocity, scale down by elasticity factor  */
  113.       end
  114.     call meter_step
  115. end
  116. call close mot
  117. call meter_end
  118. call notify(1,'!Created LightWave Motion File: 'File.name)
  119. exit
  120.  
  121. ax: procedure
  122.   arg x,y,z
  123.   return(0)
  124.  
  125. ay: procedure expose g
  126.   arg x,y,z
  127.   return(-g)
  128.  
  129. az: procedure
  130.   arg x,y,z
  131.   return(0)
  132.  
  133. WriteKey:  PROCEDURE  EXPOSE x y z h p b xsc ysc zsc
  134.   arg MotFile, frame, lin
  135.   channels=x y z h p b xsc ysc zsc
  136.   spline=frame lin "0.0 0.0 0.0"
  137.   say channels spline
  138.   call writeln(MotFile,channels)
  139.   call writeln(MotFile,spline)
  140.   return frame
  141.  
  142. WriteHeader: PROCEDURE EXPOSE File.
  143.   arg MotFile, Keys
  144.   if open(MotFile,File.name,'W') then do
  145.     call writeln(MotFile,File.type)
  146.     call writeln(MotFile,"1") /* magic # */
  147.     call writeln(MotFile,"9") /* Channels */
  148.     call writeln(MotFile,Keys)
  149.   end
  150.   else do
  151.     Bummer("Can't open Motion file : "File.name)
  152.   end
  153.   return 1
  154.  
  155. Bummer:
  156.  ARG etxt
  157.     t=Notify(1,'!Rexx Script Error','@'ETxt)
  158.  exit
  159.  
  160. syntax:
  161. error:
  162.   call end_all
  163.     t=Notify(1,'!Rexx Script Error','@'ErrorText(rc),'Line 'SIGL)
  164.     exit
  165.